/**
 * 业务小工具
 */
import { isEmpty } from './BaseUtil';
import { BASE64Helper } from './CryptUtil.js';

const TOKEN = 'token';
const USER_INFOT = 'userinfo';
const TENANT_ID = 'tenantId';

/**
 * 保存token
 */
export const setToken = token => {
    localStorage.setItem(TOKEN, token)
}

/**
 * 获取token
 */
export const getToken = () => {
    try {
        return localStorage.getItem(TOKEN)
    } catch (err) {
        return ''
    }
}

/**
 * 清除token
 */
export const removeToken = ()=>{
    localStorage.clear()
}

/**
 * 保存用户数据
 */
export const saveUserInfo = (userinfo = {}) => {
    if (isEmpty(userinfo)) {
        return
    }
    localStorage.setItem(USER_INFOT, BASE64Helper.encode(JSON.stringify(userinfo)))
}

/**
 * 获取用户数据
 */
export const loadUserinfo = () => {
    try {
        return JSON.parse(BASE64Helper.decode(localStorage.getItem(USER_INFOT)))
    } catch (err) {
        return {}
    }
}


/**
 * 租户
 */
export const getTenantId = () => {
    try {
        return localStorage.getItem(TENANT_ID)
    } catch (err) {
        return ''
    }
}

/**
 * 设置租户
 */
export const setTenantId = tenantId => {
    console.log('tenantId',tenantId)
    localStorage.setItem(TENANT_ID, tenantId)
}

/**
 * 移除租户
 */
export const removeTenantId = () => {
    localStorage.removeItem(TENANT_ID)
}
